package com.chongdong.config.datasource;

import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * 药生所saas平台数据源配置
 */

@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = {"com.chongdong.dao.huali.mapper"}, sqlSessionFactoryRef = "hualiSqlSessionFactory")
public class HuaLiDataSourceConfig {

    @Bean(name = "hualiDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.huali")
    @Primary
    DataSource imbDataSource() {
        return new DruidDataSource();
    }

    @Bean(name = "hualiSqlSessionFactory")
    @Primary
    public SqlSessionFactory imbSqlSessionFactory(@Qualifier("hualiDataSource") DataSource imbDataSource) {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(imbDataSource);
        bean.setTypeAliasesPackage("com.chongdong.entity.huali");
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            bean.setMapperLocations(resolver.getResources("classpath:mybatis/mapper/huali/*.xml"));
            return bean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Bean(name="hualiTransactionManager")
    @Primary
    public DataSourceTransactionManager imbTransactionManager() {
        return new DataSourceTransactionManager(imbDataSource());
    }
    /*@Bean(name="imbMapperScannerConfigurer")
    public MapperScannerConfigurer imbMapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("imbSqlSessionFactory");
        mapperScannerConfigurer.setBasePackage("com.chongdong.dao.imb.mapper");
        mapperScannerConfigurer.setMarkerInterface(Mapper.class);
        Properties properties = new Properties();
        properties.setProperty("notEmpty", "false");
        properties.setProperty("IDENTITY", "MYSQL");
        mapperScannerConfigurer.setProperties(properties);
        return mapperScannerConfigurer;
    }*/
    @Bean(name="hualiPageHelper")
    @Primary
    public PageHelper hualiPageHelper(){
        PageHelper pageHelper=new PageHelper();
        Properties p=new Properties();
        p.setProperty("offsetAsPageNum","true");
        p.setProperty("dialect","mysql");
        p.setProperty("rowBoundsWithCount", "false");
        pageHelper.setProperties(p);
        return pageHelper;
    }
}
